
package com.probiz.estoresa.customer.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Feedback;
import com.probiz.estore.common.model.system.AppUser;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.model.FeedbackSearchCriteria;
import com.probiz.estore.customer.service.FeedbackManager;
import com.probiz.estore.customer.util.RegularExpressionsValidate;
import com.probiz.estore.system.service.MsgSendManager;
import com.probiz.estore.system.util.MsgTemplateConstants;
import com.probiz.estore.webapp.util.RequestContext;
import com.probiz.estore.webapp.util.RequestUtil;

public class FeedbackController extends GenericStoreAdminController<Feedback> {
	private FeedbackManager		feedbackManager	= null;
	private MsgSendManager   msgSendManager = null;
	private static final String	Feedback_LIST	= "feedbackList";

	public void setFeedbackManager(FeedbackManager feedbackManager) {
		this.feedbackManager = feedbackManager;
	}
	
	public void setMsgSendManager(MsgSendManager msgSendManager) {
		this.msgSendManager = msgSendManager;
	}

	@Override
	protected String getEntityName(Feedback entity) {
		return null;
	}




	@Override
	protected void initController() throws Exception {
		mgr = feedbackManager;
	}

	@Override
	protected void preSave(HttpServletRequest request, Feedback entity, BindException errors) {
		
	}
	
	
	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/feedbackList");
		return mv;
	}
	
	public ModelAndView search(HttpServletRequest request,	HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/include/feedbackListBody");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		FeedbackSearchCriteria feedbackSearchCriteria = new FeedbackSearchCriteria();
		try {
			// 绑定参数
			bind(request, feedbackSearchCriteria);
			List<Feedback> feedbackList = feedbackManager.searchByCriteriaForController(feedbackSearchCriteria,searchCriteria);
			mv.addObject("feedbackList", feedbackList);
			mv.addObject("feedbackSearchCriteria", feedbackSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	/**
	 * 切换已阅未阅
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiToggleReadStatus(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				Feedback feedback = feedbackManager.getById(Integer.valueOf(entityId));
				if(!feedback.getIsRead().equals(Feedback.STATUS_READ)){
					feedbackManager.doMarkRead(feedback);
				}else{
					feedbackManager.doMarkUnRead(feedback);
				}
			}
		}
		return null;
	}
	/**
	 * 切换可见隐藏
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiToggleShowStatus(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				Feedback feedback = feedbackManager.getById(Integer.valueOf(entityId));
				if(!feedback.getStatus().equals(Feedback.STATUS_SHOW)){
					feedbackManager.doMarkShow(feedback);
				}else{
					feedbackManager.doMarkUnShow(feedback);
				}
			}
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				Feedback feedback  = feedbackManager.getById(Integer.valueOf(entityId));
				feedbackManager.deleteFully(feedback);
			}
		}
		return null;
	}
	/**
	 * 置为已读
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView markRead(HttpServletRequest request,	HttpServletResponse response){
		Map<String, Object> data = new HashMap<String, Object>();
		Feedback feedback = feedbackManager.getById(RequestUtil.getInteger(request, "feedbackId"));
		if(!feedback.getIsRead().equals(Feedback.STATUS_READ)){
			feedbackManager.doMarkRead(feedback);
		}
		data.put("feedbackId", feedback.getFeedbackId());
		return this.getModelAndAjaxJsonView(data);
	}
	/**
	 * 管理员回复
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView replyFeedback(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Feedback feedback = feedbackManager.getById(RequestUtil.getInteger(request, "feedbackId"));
		Feedback reply = new Feedback();
		reply.setParent(feedback);
		reply.setCustomer((AppUser) RequestContext.getCurrentUser());
		reply.setRemoteIp(request.getRemoteAddr());
		reply.setMessage(RequestUtil.getParameterNullSafe(request, "replyMessage"));
		reply.setIsRead(Feedback.STATUS_READ);
		reply.setStatus(Feedback.STATUS_SHOW);
		feedbackManager.saveReply(reply);
		String contact = feedback.getContact();
		Map<String, Object> model = new HashMap<String, Object>();
		if(null != feedback.getCustomer()){
			feedback.setCustomerName(feedback.getCustomer().getUsername());
		}
		model.put("feedBack", feedback);
		model.put("reply", reply);
		if(null != feedback.getCustomer()){
			msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_FEEDBACK_REPLY, model, new String[]{feedback.getCustomer().getEmail()}, new String[]{feedback.getCustomer().getAppuserId().toString()}, new String[]{feedback.getCustomer().getMobilephone()});
		}else{
			if(null != contact&&RegularExpressionsValidate.isEmail(contact)){
				msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_FEEDBACK_REPLY, model, new String[]{contact}, null, null);
			}
			if(null != contact&&RegularExpressionsValidate.isMobileNO(contact)){
				msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_FEEDBACK_REPLY, model, null, null, new String[]{contact});
			}
		}
		data.put("feedbackId", feedback.getFeedbackId());//返回反馈的id
		return this.getModelAndAjaxJsonView(data);
	}
	/**
	 * 获得反馈
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView getFeedback(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Feedback feedback = feedbackManager.getById(RequestUtil.getInteger(request, "feedbackId"));
		data.put("feedbackId", feedback.getFeedbackId());
		data.put("replyCount", feedback.getReplyCount());
		data.put("onShowReplyCount", feedback.getOnShowReplyCount());
		return this.getModelAndAjaxJsonView(data);
	}

	

}
